
//3298.移山所需的最小秒数
class Solution {

    //计算所有工人在t内降低的高度
    long long totalHeight(vector<int>& nums,long long t)
    {
        long long ret=0;
        for(auto e:nums) ret+=height(e,t);
        return ret;
    }

    //计算工人在时间t内降低的高度
    long long height(int worktime,long long t)
    {
        long long k=t/worktime;
        long long len=(-1+sqrt(1+8*k))/2;
        return len;
    }

public:
    long long minNumberOfSeconds(int mountainHeight, vector<int>& workerTimes) {
        //当时间越多降低的高度越长，可以使用二分解决
        long long left=0,right=0;
        long long heig=0,mintime=ranges::min(workerTimes),each=0;
        //确定上边界right
        while(heig<mountainHeight)
        {
            each+=mintime;
            right+=each;
            heig++;
        }

        while(left+1<right)
        {
            long long mid=left+(right-left)/2;
            if(totalHeight(workerTimes,mid)>=mountainHeight) right=mid;
            else left=mid;
        }
        return right;
    }
};